#include <stdio.h>

int binary_search (int *array, int size, int find)
{
	int mid, low, high;
	
	low = 0;
	high = size-1;
//	mid = (low+high)/2;
	
	while (low <= high)
	{
		mid = (low+high)/2;

		if (find < array[mid])
			high = mid-1;
		else if (find > array[mid])
			low = mid+1;
		else if (find == array[mid])
			return mid;
	}
	printf ("\nafter while loop");
	return -1;
}

int main(void)
{
	int list[10]={0}, size, find, i, r;

	printf ("Enter array size :\t");
	scanf ("%d", &size);

//	printf ("\nEnter elements");
	for (i=1; i<=size; i++)
	{
		printf ("\nEnter %d element :\t", i);
		scanf ("%d", &list[i]);
	}
	
	printf ("\nEnter key element to find :\t");
	scanf ("%d", &find);
	
	printf ("\nbefore binary_search");
	r = binary_search (list, size, find);
	printf ("\nafter binary_search");
	if (r == -1)
		printf ("\nGiven key is not found");
	else
		printf ("\nGiven key found at %d location, key is %d", r, find);
	
	return 0;
}
